package com.gelian.demo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gelian.library.model.GlDevice;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class AdapterDeviceList extends BaseAdapter {

    private Context context;
    private List<GlDevice> deviceList;

    public static String lastClickId;

    public AdapterDeviceList(Context context, List<GlDevice> deviceList) {
        this.context = context;
        this.deviceList = deviceList;
    }

    /**
     * 初始化条目的状态
     *
     * @param holder
     * @param device
     * @param viewType
     */
    private void initState(ViewHolder holder, GlDevice device, int viewType) {
        String gl_name = device.getGl_name();
        String imei = device.getImei();

        if (TextUtils.isEmpty(gl_name)) {
            imei = imei.substring(imei.length() - 4);
            gl_name = "GL-开发板-" + imei;
        }

        holder.deviceNameTvItem.setText(gl_name);

        holder.deviceOnlineIv.setImageResource(R.mipmap.ic_offline_item);
        holder.deviceOnlineTv.setText("离线");
        if (device.getOnline_status()) {
            holder.deviceOnlineIv.setImageResource(R.mipmap.ic_online_item);
            //设置设备是局域网还是远程
            if (device.getLocal()) {
                holder.deviceOnlineTv.setText("局域网");
            } else {
                holder.deviceOnlineTv.setText("远程");
            }
        }
    }

    /**
     * 条目的点击事件
     * <p>
     * RecyclerView没有提供每个条目的点击事件，
     * 如果习惯像listview的使用或者适配器是公用的话，可以自己写个接口回调在Activity或者Fragment中再去实现
     *
     * @param holder
     * @param device
     * @param viewType
     */
    private void initClick(ViewHolder holder, final GlDevice device, int viewType) {
        holder.itemView.setOnClickListener(null);

        /**
         * 每个条目的点击事件
         */
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lastClickId = device.getImei();
                IntentManager.goToDeviceActivity(context, device);
            }
        });


    }

    @Override
    public int getCount() {
        return deviceList == null ? 0 : deviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        int viewType = getItemViewType(position);

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_device_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        GlDevice device = deviceList.get(position);
        initState(holder, device, viewType);
        initClick(holder, device, viewType);
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'device_item_g1.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.device_img_iv_item)
        ImageView deviceImgIvItem;
        @Bind(R.id.device_name_tv_item)
        TextView deviceNameTvItem;
        @Bind(R.id.device_type_tv_item)
        TextView deviceTypeTvItem;
        @Bind(R.id.device_online_tv_tiem)
        TextView deviceOnlineTv;
        /**
         * 是局域网控制，还是远程
         */
        @Bind(R.id.device_online_iv_tiem)
        ImageView deviceOnlineIv;
        @Bind(R.id.arrow_iv)
        ImageView arrowIv;

        View itemView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

}
